<?php defined('SYSPATH') or die('No direct script access.');

class Product_Attribute_Model extends ORM 
{
	public function getRelationedAttribute($type_id, $used = TRUE)
	{
		//select a.* from attribute a, product_type_attribute pta, product_type pt where a.id=pta.attribute_id and pt.id=2;
		//select a.* from attribute a left join product_type_attribute pta on a.id=pta.attribute_id where pta.attribute_id is null;;
		//select a.* from attribute a where  a.id  not in (select attribute_id from product_type_attribute where product_type_id=2);

		//return $this->select('attribute.*')->join('product_type_attribute AS pta', 'attribute.id=pta.attribute_id')
		//							->where('pta.product_type_id=<>'.$type_id)
		//							->where('pta.attribute_id is '.($used == TRUE ? 'not' : '').' null')->find_all();
		return $this->select('product_attributes.*')->where('product_attributes.id '.($used == TRUE ? '' : 'not').' in (select attribute_id from product_type_attributes where product_type_id='.$type_id.')')
								->join('product_type_attributes AS pta', 'product_attributes.id=pta.attribute_id', NULL, 'LEFT')
								->orderby('pta.sort', 'asc')->find_all();
	}
}
